package com.aaa.controller.front;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.aaa.controller.BaseController;
import com.aaa.dao.fpa.TesterFpaDao;
import com.aaa.entity.*;
import com.aaa.entity.fpa.PersonalityEntity;
import com.aaa.entity.fpa.TestResultFpa;
import com.aaa.entity.fpa.TesterFpa;
import com.aaa.entity.mht.TestResultMht;
import com.aaa.entity.mht.TesterMht;
import com.aaa.entity.sas.QuestionSas;
import com.aaa.entity.sas.TestResultSas;
import com.aaa.entity.sas.TesterSas;
import com.aaa.entity.mht.QuestionMht;
import com.aaa.entity.sas.QuestionSas;
import com.aaa.entity.sas.TestResultSas;
import com.aaa.entity.sas.TesterSas;
import com.aaa.entity.mht.TestResultMht;
import com.aaa.entity.mht.TesterMht;
import com.aaa.entity.scl.QuestionScl;
import com.aaa.entity.scl.TestResultScl;
import com.aaa.entity.scl.TesterScl;
import com.aaa.entity.TesterVo;
import com.aaa.entity.sds.QuestionSds;
import com.aaa.entity.sds.TestResultSds;
import com.aaa.entity.sds.TesterSds;
import com.aaa.service.common.TestPlanService;
import com.aaa.service.fpa.PersonalityService;
import com.aaa.service.fpa.TesterFpaService;
import com.aaa.service.mht.QuestionMhtService;
import com.aaa.service.mht.TestResultMhtService;
import com.aaa.service.mht.TesterMhtService;
import com.aaa.service.sas.*;
import com.aaa.service.sas.QuestionSasService;
import com.aaa.service.sas.TestResultSasService;
import com.aaa.service.sas.TesterSasService;
import com.aaa.service.mht.TestResultMhtService;
import com.aaa.service.mht.TesterMhtService;
import com.aaa.service.scl.QuestionSclService;
import com.aaa.service.scl.TestResultSclService;
import com.aaa.service.scl.TesterSclService;
import com.aaa.service.sds.QuestionSdsService;
import com.aaa.service.sds.TestResultSdsService;
import com.aaa.service.sds.TesterSdsService;
import com.aaa.service.sds.ViewTestResultSdsService;
import com.aaa.utils.MyConstants;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
 * @author ：Username 刘亦辰（59372312@qq.com）
 * @date ：Created in 2022/9/2 19:17
 * @description：前端控制器
 * @modified By：
 * @version:
 */

@RestController
@RequestMapping("front")
public class FrontController extends BaseController {
    @Autowired
    TestPlanService testPlanService;

    @Autowired
    TesterSclService testerSclService;

    @Autowired
    TesterSasService testerSasService;

    @Autowired
    TesterMhtService testerMhtService;

    @Autowired
    TesterSdsService testerSdsService;

    @Autowired
    QuestionSclService questionSclService;

    @Autowired
    QuestionSasService questionSasService;

    @Autowired
    QuestionMhtService questionMhtService;

    @Autowired
    QuestionSdsService questionSdsService;

    @Autowired
    TestResultSclService testResultSclService;

    @Autowired
    TestResultMhtService testResultMhtService;

    @Autowired
    TestResultSasService testResultSasService;

    @Autowired
    TestResultSdsService testResultSdsService;

    @Autowired
    ViewTestResultSdsService viewTestResultSdsService;

    @Autowired
    ViewTestResultSasService viewTestResultSasService;
    @Autowired
    TesterFpaService testerFpaService;
    @Autowired
    private TesterFpaDao testerFpaDao;
    @Autowired
    PersonalityService personalityService;

    @RequestMapping("toShowQuestion.do")
    public ModelAndView toShowQuestion(ModelAndView modelAndView) {
        modelAndView.setViewName("front/testQuestion");
        return modelAndView;
    }

    /**
     * @param
     * @return com.aaa.entity.Result
     * @create by: 刘亦辰（59373212@qq.com）
     * @description:1、校验测试手机号是否已经测试过 2、校验邀请码是否合法
     * 2.1、如果没有对应的测试计划，非法
     * 2.3、过期的，或者未开始的
     * 3、根据邀请码获取所有问题列表
     * @create time: 2022/9/2 10:40
     */
    @RequestMapping("checkTest.do")
    public Result checkTest(@RequestBody TesterVo testerVo, HttpSession session) {
        //根据测试邀请码判断当前所属的测试类型，去数据库的视图view_test_plan
        TestPlan testPlan = testPlanService.queryByTestCode(testerVo.getTestCode());
        //校验邀请码是否合法
        if (ObjectUtil.isNotEmpty(testPlan)) {
            //判断时间
            Date testBegin = testPlan.getTestBegin();
            Date testEnd = testPlan.getTestEnd();
            Date now = new Date();
            if (now.before(testBegin)) {
                return fail("测试未开始");
            }
            if (now.after(testEnd)) {
                return fail("测试时间已结束，你来晚了！");
            }
            //正常情况,获取测试类型
            String type = testPlan.getType();
            testerVo.setType(type);
            testerVo.setTestCode(testPlan.getTestCode());
            testerVo.setTestName(testPlan.getTestName());
            testerVo.setTestPlanId(testPlan.getId());
            //默认手机号没有重复，false
            Boolean flag = false;
            List<Question> questionList = new ArrayList<>();
            switch (type) {
                case MyConstants.SCL:
                    TesterScl TesterScl = testerSclService.queryByPhone(testerVo.getPhone());
                    //手机号重复
                    if (ObjectUtil.isNotEmpty(TesterScl)) {
                        flag = true;
                    } else {
                        //开始测试，进入到显示所有题目的页面
                        //将所有的测试题放入到session中
                        List<QuestionScl> questionSclList = questionSclService.queryAllScl(PageSortEntity.noPage(), null);
                        //将 List<QuestionFpa>  转换成List<Question>
                        questionList = BeanUtil.copyToList(questionSclList, Question.class);
                        for (Question question : questionList) {
                            System.out.println(question);
                        }
                    }
                    break;
                case MyConstants.SAS:
                    TesterSas testerSas = testerSasService.queryByPhone(testerVo.getPhone());
                    if (ObjectUtil.isNotEmpty(testerSas)) {
                        flag = true;
                    } else {
                        List<QuestionSas> all = questionSasService.findAll(PageSortEntity.noPage(), null);
                        questionList = BeanUtil.copyToList(all, Question.class);
                        for (Question question : questionList) {
                            System.out.println(question);
                        }
                    }
                    break;
                case MyConstants.SDS:
                    //判断手机号是否重复
                    TesterSds testerSds = testerSdsService.queryByPhone(testerVo.getPhone());
                    if (ObjectUtil.isNotEmpty(testerSds)) {
                        flag = true;
                    } else {
                        //开始测试,进入到显示所有题目的页面,先跳转页面，再显示题目
                        //将所有的测试题放入到session中
                        Page page = questionSdsService.queryQuestionSdsPage(PageSortEntity.noPage(), null);
                        List<QuestionSds> questionSdsList = (List<QuestionSds>) page;
                        //将questionSdsList转换成question
                        questionList = BeanUtil.copyToList(questionSdsList, Question.class);
                        List<Question> questionList1 = BeanUtil.copyToList(questionSdsList, Question.class);
                        session.setAttribute("tester",testerVo);
                        session.setAttribute("questionList",questionList1);
                        return success();
                    }
                    break;
                case MyConstants.MHT:
                    TesterMht testerMht=testerMhtService.queryByPhone(testerVo.getPhone());
                    if (ObjectUtil.isNotEmpty(testerMht)){
                        flag=true;
                    }else
                    {
                        List<QuestionMht> all = questionMhtService.queryMhtAll();
                        questionList = BeanUtil.copyToList(all, Question.class);
                        for (Question question : questionList) {
                            System.out.println(question);
                        }
                    }
                case MyConstants.FPA:
                    TesterFpa testerFpa =testerFpaService.queryByPhone(testerVo.getPhone());
                    //手机号重复
                    if(ObjectUtil.isNotEmpty(testerFpa)){
                        flag=true;
                    }else{
                        //开始测试，进入到显示所有题目的页面
                        //将所有的测试题放入到session中
                        Page page =personalityService.queryPersonalitAlla(PageSortEntity.noPage(), null);
                        System.out.println(page);
                        //将 List<QuestionFpa>  转换成List<Question>
                        List<PersonalityEntity> questionSdsList=(List<PersonalityEntity>)page;
                        questionList = BeanUtil.copyToList(questionSdsList, Question.class);
                    }
                    break;
                default:
                    return fail("不识别的测试类型");
            }
            if (flag) {
                return fail("手机号已经做过测试了亲");
            }
            //保存tester到session中
            session.setAttribute("tester", testerVo);
            session.setAttribute("questionList", questionList);
            return success();
        } else {
            return fail("测试邀请码无效");
        }
    }

    @PostMapping("saveBatchTestResult.do")
    public Result saveBatchTestResult(@RequestBody List<TestResultVo> testResultVoList, HttpSession session) {
        TesterVo tester = (TesterVo) session.getAttribute("tester");
        String type = tester.getType();
        switch (type) {
            case MyConstants.SCL:
                //1、根据测试类型进行保存测试者的信息  TesterScl
                TesterScl testerScl = new TesterScl();
                BeanUtil.copyProperties(tester, testerScl);
                testerSclService.insert(testerScl);
                //testerScl中自动包含主键信息
                //2、根据测试类型进行保存答案
                /*System.out.println(testResultVoList.size());*/
                List<TestResultScl> testResultSclList = BeanUtil.copyToList(testResultVoList, TestResultScl.class);
                //填充测试者的id
                for (TestResultScl testResultScl : testResultSclList) {
                    testResultScl.setTesterId(testerScl.getId());
                }
                testResultSclService.insertBatch(testResultSclList);
                return success();
            case MyConstants.SAS:
                //1、根据测试类型进行保存测试者的信息
                TesterSas testerSas = new TesterSas();
                BeanUtil.copyProperties(tester, testerSas);
                testerSasService.insert(testerSas);
                //2、根据测试类型进行保存答案
                List<TestResultSas> testResultSasList = BeanUtil.copyToList(testResultVoList, TestResultSas.class);
                for (TestResultSas testResultSas : testResultSasList) {
                    testResultSas.setTesterId(testerSas.getId());
                }
                testResultSasService.insertBatch(testResultSasList);
                Integer id1 = testerSas.getId();
                String s1 = viewTestResultSasService.queryAllSasById(id1);
                Map map1 = new HashMap();
                map1.put("s", s1);
                return success(map1);
            case MyConstants.SDS:
                //1、根据测试类型进行保存测试者的信息
                TesterSds testerSds = new TesterSds();
                BeanUtil.copyProperties(tester, testerSds);
                testerSdsService.insert(testerSds);
                //2、根据测试类型进行保存答案
                List<TestResultSds> testResultSdsList = BeanUtil.copyToList(testResultVoList, TestResultSds.class);
                for (TestResultSds testResultSds : testResultSdsList) {
                    testResultSds.setTesterId(testerSds.getId());
                }
                testResultSdsService.insertBatch(testResultSdsList);
                Integer id = testerSds.getId();
                String s = viewTestResultSdsService.querySdsById(id);
                Map map = new HashMap();
                map.put("s", s);
                return success(map);
            case MyConstants.MHT:
                //1、根据测试类型进行保存测试者的信息  TesterMht
                TesterMht testerMht = new TesterMht();
                BeanUtil.copyProperties(tester, testerMht);
                testerMhtService.insert(testerMht);
                //testerScl中自动包含主键信息
                //2、根据测试类型进行保存答案
                /*System.out.println(testResultVoList.size());*/
                List<TestResultMht> testResultMhtList = BeanUtil.copyToList(testResultVoList, TestResultMht.class);
                //填充测试者的id
                for (TestResultMht testResultMht : testResultMhtList) {
                    testResultMht.setTesterId(testerMht.getId());
                }
                testResultMhtService.insertBatch(testResultMhtList);
            case MyConstants.FPA:
                //1、根据测试类型进行保存测试者的信息  TesterScl
                TesterFpa testerFpa = new TesterFpa();
                BeanUtil.copyProperties(tester,testerFpa);
                testerFpaService.addQuery(testerFpa);
                // 根据测试类型进行保存答案
                List<TestResultFpa> testResultFpaList = BeanUtil.copyToList(testResultVoList, TestResultFpa.class);
                // 填充测试者id
                Integer ida = testerFpaDao.selectUserName(testerFpa.getName());
                testerFpa.setId(ida);
                for(TestResultFpa testResultFpa : testResultFpaList ){
                    testResultFpa.setTesterId(testerFpa.getId());
                }
                testerFpaService.insertBatch(testResultFpaList);

                return testerFpaService.seleResult(ida);
            default:
                return fail("不识别的测试类型");
        }
    }
}